package com.qzw.network.netty.client.config;

import com.alibaba.fastjson.JSONObject;
import com.qzw.network.netty.client.handler.NettyClientHandler;
import com.qzw.network.netty.client.model.User;
import com.qzw.network.netty.common.util.Msg;
import com.qzw.network.netty.common.util.MsgUtils;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ：quziwei
 * @date ：Created in 2020/6/10 6:24 下午
 * @description：客户端启动
 */
@Slf4j
@Data
public class NettyClientStart implements Runnable {
    static final String HOST = System.getProperty("host", "192.168.31.90");
    static final int PORT = Integer.parseInt(System.getProperty("port", "9999"));
    static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));

    private String content;

    public NettyClientStart(String content) {
        this.content = content;
    }

    @Override
    public void run() {
        EventLoopGroup group = new NioEventLoopGroup();
        try {

            int num = 0;
            boolean boo = true;

            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .handler(new NettyClientChannelInitializer() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("decoder", new StringDecoder());
                            p.addLast("encoder", new StringEncoder());
                            p.addLast(new NettyClientHandler());
                        }
                    });

            ChannelFuture future = b.connect(HOST, PORT).sync();
            User user = new User();
            user.setUserId(new AtomicInteger(1));
            future.channel().writeAndFlush(JSONObject.toJSONString(MsgUtils.result("000", new Byte("0"), "", user)));

//            while (boo) {
//
//                num++;
//
////                future.channel().writeAndFlush("我是一个客户端");
//                User user = new User();
//                user.setUserId(new AtomicInteger(1));
//                future.channel().writeAndFlush(JSONObject.toJSONString(MsgUtils.result("000",new Byte("0"),"",user)));
//
//                try { //休眠一段时间
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//                //每一条线程向服务端发送的次数
//                if (num == 100) {
//                    boo = false;
//                }
//            }

            log.info(content + "-----------------------------" + num);
            //future.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
}


